package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Override
    public void insert(SetmealDTO setmealDTO) {
        //先插入setmeal表
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);
        //再插入setmeal_dish表
        List<SetmealDish> setmealDishes=setmealDTO.getSetmealDishes();
        for (SetmealDish dish : setmealDishes) {
            dish.setSetmealId(setmeal.getId());
        }
        setmealDishMapper.insert(setmealDishes);
    }

    @Override
    public PageResult getpage(SetmealPageQueryDTO setmealPageQueryDTO) {
        //调用PageHelper
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        //调用函数
        List<SetmealVO> list=setmealMapper.getPage(setmealPageQueryDTO);
        //将其封装解析
        Page<SetmealVO> p= (Page<SetmealVO>) list;
        return new PageResult(p.getTotal(),p.getResult());
    }

    @Transactional
    @Override
    public void update(SetmealDTO setmealDTO) {
        //先修改setmeal表
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        Long id = setmealDTO.getId();
        //在删除setmeal_dish表
        setmealDishMapper.delet(id);
        List<SetmealDish> setmealDishes=setmealDTO.getSetmealDishes();
        if(setmealDishes!=null&& !setmealDishes.isEmpty()){
            for (SetmealDish dish : setmealDishes) {
                dish.setSetmealId(id);
            }
            setmealDishMapper.insert(setmealDishes);
        }


    }

    @Override
    public SetmealVO getById(Long id) {
        //先查询setmeal表
        SetmealVO setmealVO=setmealMapper.getById(id);
        //在查询setmeal_dish表
        List<SetmealDish> setmealDishes=setmealDishMapper.getAllById(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        Setmeal setmeal=Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }

    @Transactional
    @Override
    public void delete(List<Long> ids) {
        for (Long id : ids) {
            //先判断是否在售卖中
            SetmealVO setmeal=setmealMapper.getById(id);
            if(setmeal.getStatus()==1){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
            //最后删除
            setmealMapper.delet(id);
            setmealDishMapper.delet(id);
        }
    }

    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
